// pages/inquiry/create.js
const app = getApp();
const util = require('../../utils/util')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    longitude: "",
    latitude: "",
    customerCompanyBusinessEiid: '',
    fields: [], //自定义字段列表
    files: [],
    inquiryType: 0, //默认自动询价
    submitFlag: 1, // 立即提交
    subjectIds: [], //评估对象id列表

    // 业务列表
    eiidList:[],
    index: '0'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getConfigField()
    this.getEiidList()
  },

  //跳转新增对象
  linkCreate(e){
    let { type } = e.currentTarget.dataset
    wx.navigateTo({
      url: './subject?type=' + type,
    })
  },

  // 提交整个表单
  submitForm(){
    console.log(this.data)
    let { name, longitude,latitude, inquiryType, fields, customerCompanyBusinessEiid, subjectIds, files, submitFlag  } = this.data
    fields = fields.map(item=>{
      let params = {
        fieldId: item.id
      }
      if(item.valueType==2){ //字符串
        params.stringValue = item.stringValue
      }
      return params
    })
    subjectIds = subjectIds.map(item=>item.id)
    let temp = {name, longitude,latitude, inquiryType, fields, customerCompanyBusinessEiid, subjectIds, files, submitFlag};
    let params = util.transUrlData(temp)
    app.request({
      url: '/inquiry/create' + params,
      success: r=>{
        if(r.code==0){
          app.toast('创建成功,等待评估结果')
          wx.switchTab({
            url: '/pages/tabBar/home',
          })
        }
      }
    })
  },

  // 自定义字段
  changeInput(e){
    let { index } = e.currentTarget.dataset;
    let { value } = e.detail;
    let fields = this.data.fields.map((item, index2)=>{
      if(index==index2){
        if(item.valueType==2){
          item.stringValue = value
        }
      }
      return item;
    })
    this.setData({ fields })
  },

  // 修改项目名称
  changName(e){
    this.setData({ name: e.detail.value })
  },

  //修改业务类型
  bindPickerChange(e){
    let { value } = e.detail
    let id = this.data.eiidList[value].value;
    this.setData({ customerCompanyBusinessEiid: id, index: value })
  },

  //获取业务类型
  getEiidList(){
    app.request({
      url: "/enum/info?id=36",
      success: r=>{
        if(r.code==0){
          let eiidList = r.data.items.map(item=>{
            return { label: item.name, value:item.id }
          })
          this.setData({ eiidList, customerCompanyBusinessEiid: eiidList[0].value })

        }
      }
    })
  },

  //获取字段配置
  getConfigField(){
    app.request({
      url: '/field/getList?objectType=11&pageCount=999&templateFlag=0',
      success: r=>{
        if(r.code == 0){
          let fields = r.data.list;
          this.setData({ fields })
        }
      }
    })
  },

  handleSelecPoint(){
    wx.navigateTo({
      url: './map',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})